<script setup>
import { ref } from 'vue'
import router from '@/router'
import { useAdminStore } from '@/stores/adminStore'
import { User, Lock } from '@element-plus/icons-vue'

const adminStore = useAdminStore()

const loginData = ref({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 5, max: 16, message: '长度应为5-16个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 5, max: 16, message: '长度应为5-16个字符', trigger: 'blur' }
  ]
}

const form = ref(null)

const doLogin = () => {
  form.value.validate(async (valid) => {
    if (valid) {
      const { username, password } = loginData.value
      await adminStore.getAdminInfo({ username, password })
      if (adminStore.adminInfo.code === 0) {
        ElMessage.success(adminStore.adminInfo.data.message || '登录成功')
        router.push('/admin')
      } else {
        ElMessage.error('登录失败')
      }
    }
  })
}
</script>

<template>
  <div class="login-container">
    <el-form 
      ref="form" 
      class="login-form"
      :model="loginData" 
      :rules="rules"
      size="large"
    >
      <h1 class="title">管理员登录</h1>
      
      <el-form-item prop="username">
        <el-input 
          :prefix-icon="User" 
          placeholder="请输入用户名"
          v-model="loginData.username"
        />
      </el-form-item>

      <el-form-item prop="password">
        <el-input 
          :prefix-icon="Lock" 
          type="password"
          placeholder="请输入密码"
          show-password
          v-model="loginData.password"
        />
      </el-form-item>

      <el-form-item>
        <el-button 
          class="login-btn" 
          type="primary" 
          @click="doLogin"
        >
          立即登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f2f5;
}

.login-form {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin-bottom: 30px;
  color: #2c3e50;
}

.login-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
  letter-spacing: 2px;
}

:deep(.el-input__wrapper) {
  height: 44px;
}
</style>